//创建获取用户信息的action
import { http } from '@/utils/http'
import type { RootThunkAction } from '@/types/store'
import type {
	UserResponse,
	UserProfileResponse,
	UseProfile,
	UserPhotoResponse,
} from '@/types/data'

//用户接口响应类型
// type UserResponse = {
// 	message: string
// 	data: User
// }

//我的页面 - 获取个人信息
export const getUser = (): RootThunkAction => {
	return async (dispatch) => {
		const res = await http.get<UserResponse>('/user')
		// console.log(res)
		const { data } = res.data

		//存储redux
		dispatch({ type: 'profile/getUser', payload: data })
	}
}

//获取用户个人资料
export const getUseProfile = (): RootThunkAction => {
	return async (dispatch) => {
		const res = await http.get<UserProfileResponse>('/user/profile')
		//存储redux
		dispatch({ type: 'profile/getUseProfile', payload: res.data.data })
	}
}

//修改用户数据
export const updateUserProfile = (
	// 参数为 UserProfile 中的任意属性，也就是调用该 action 时，可以传入任意的用户信息
	// 从而来实现该接口的复用
	userProfile: Partial<UseProfile>
): RootThunkAction => {
	return async (dispatch) => {
		await http.patch('/user/profile', userProfile)

		//分发action更新用户昵称
		dispatch({ type: 'profile/update', payload: userProfile })
	}
}

// 更新头像
export const updatePhoto = (file: FormData): RootThunkAction => {
	return async (dispatch) => {
		const res = await http.patch<UserPhotoResponse>('/user/photo', file)
		// console.log(res)
		dispatch({ type: 'profile/update', payload: res.data.data.photo })
	}
}


